package com.example.chessbattleplatform.web.StorageIo.controller;

import com.example.chessbattleplatform.Pojo.Exception.BusinessException;
import com.example.chessbattleplatform.Pojo.Exception.StatusCode;
import com.example.chessbattleplatform.Pojo.Result.WebResult;
import com.example.chessbattleplatform.utils.ThreadJwtParseUtils;
import com.example.chessbattleplatform.web.StorageIo.Pojo.Vo.DeleteFileVO;
import com.example.chessbattleplatform.web.StorageIo.Pojo.Vo.UserFileVo;
import com.example.chessbattleplatform.web.StorageIo.service.impl.UserFileServiceInterface;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 当前业务 对象存储 控制层
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class XIANGQIStorageController {


    private final UserFileServiceInterface userFileServiceInterface;
    private final ThreadJwtParseUtils threadJwtParseUtils;
    /**
     * 添加用户图片 和 修改用户图片 共用一个接口
     * 有两种可能
     * 1.用户没用图片
     * 就直接添加图片
     * 2.用户有图片
     * 先删除用户图片在添加图片
     **/
    @PostMapping("/uploadFile")
    public WebResult<?> uploadFile(@NotNull(message = "请选择一个文件上传") @RequestParam("file") MultipartFile file) {
        // 获取线程本地存储
        Long userId = threadJwtParseUtils.getThreadUserId();

        UserFileVo userFileVo = userFileServiceInterface.uploadFile(file, userId);
        return WebResult.success(userFileVo);
    }

    // 获取用户图片
    @GetMapping("/getPicture")
    public WebResult<?> getPicture() {
        // 获取线程本地存储
        Long userId = threadJwtParseUtils.getThreadUserId();

        UserFileVo userFileVo = userFileServiceInterface.selectByUuid(userId);

        return WebResult.success(userFileVo);
    }

    // 删除用户图片
    @DeleteMapping("/DeletePicture")
    public WebResult<?> DeletePicture(@NotNull(message = "缺失参数") boolean ConfirmDelete) {
        if (!ConfirmDelete) {
            throw new BusinessException(StatusCode.MISSING_PARAMETER, "参数错误");
        }
        // 获取线程本地存储
        Long userId = threadJwtParseUtils.getThreadUserId();

        DeleteFileVO deleteFileVO = userFileServiceInterface.deleteFile(userId);

        return WebResult.success(deleteFileVO);
    }

}
